<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Blog/classes/PhpFox_ServiceBlog.class.php, $Author:  $ */



/** Service for working with comments
 *
 * @package module.comments
 * @version $Id: PhpFox_ServiceBlog.class.php 308 2008-02-11 15:19:38Z phpfoxsv $
 *
 */
class PhpFox_Mod_Blog_ServiceBlog extends BaseService
{
    function deleteBlog($iBlogId)
    {
        $oBlogItem = &$this->_oModule->getItem('Blog');
        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */
        $oCommentService = &App::getModuleService('Comments', 'Comment');
        /* @var $oCommentService PhpFox_Mod_Comments_ServiceComment */
		$oAccountService = &App::getModuleService('Account', 'Account');
        
        $iBlogId = intval($iBlogId);
        if($oBlogItem->load($iBlogId))
        {
        	if( ($oBlogItem->aData['user_id'] == App::getUser('id') ) OR App::isStaff() )
           	{
               	$oBlogItem->delete($iBlogId);
                $oCommentService->onItemDelete($iBlogId, 'blog');
                $oAccountService->changeUserInfo($oAccountService->getById($oBlogItem->aData['user_id']), 'journal', -1);
            }
        }
        return true;
    }

    /** Returns bloggers list from database
     *
     * @param integer $iCnt
     * @return array
     */
    function getTopBloggersList($iCnt)
    {
        $oBlogService = &$this->_oModule->getService('Blog');
        /* @var $oBlogService PhpFox_Mod_Blog_ServiceBlog */
        $oBlogItem = $this->_oModule->getItem('Blog');
        /* @var $oBlogItem PhpFox_Mod_Blog_ItemBlog */

        $iCnt = intval($iCnt);
        $sSql = 'SELECT COUNT(*) AS cnt, j.user_id, u.user '.
               ' FROM '.App::getT('journal').' AS j '.
               ' LEFT JOIN '. App::getT('user') .' AS u ON(j.user_id = u.id) '.
               ' GROUP BY j.user_id '.
               ' ORDER BY cnt DESC'.
               ' LIMIT '.$iCnt;

        return $oBlogItem->oDb->getRows($sSql);
    }
    
    function onDeleteAccount($aParams)
    {
    	$aIds = $aParams['aIds'];
        if ($aIds)
        {
            $oAcc = $aParams['oAcc'];
            $oBlogItem = $this->_oModule->getItem('Blog');
            foreach ($aIds as $iUserId)
	        {
	            $sSql = 'Select id FROM '.App::getT('journal').' WHERE user_id=\''. $iUserId .'\' ';
		        $aBlogs = $oBlogItem->oDb->getRows($sSql);
	            foreach ($aBlogs as $aBlog)
	            {
	                $this->deleteBlog($aBlog['id']);
	            }
	        }
        }
    }
}
?>